/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function CarRent() {
    this.javaClass = "limousine.vo.CarRent"; 
    
    this.tableId = "";
    this.carRentRateId ="";
    this.perHour ="";
    this.minimumCharge ="";
    this.sedan ="";
    this.van ="";
    this.trip ="";
    this.serviceType ="";
    this.createBy ="";
    this.createDate ="";
    this.updateBy ="";
    this.updateDate ="";
   
}
function openLookup() {
    var body = $d("_body");
    body.innerHTML = requestPage("lookup.main.jsp");

    var x = new Driver();

    x.tableId
}
function edit(id) {
    try {        
        formEntry();
        document.getElementById("fieldSetLabel").textContent="Car Rent Edit";
        var o = service.findById(id);
                               
        $d("tableId").value = o.tableId;
        $d("carRentRateId").value = o.carRentRateId;
        $d("perHour").value = o.perHour;
        $d("minimumCharge").value = o.minimumCharge;
        $d("sedan").value = o.sedan;
        $d("van").value = o.van;
        $d("trip").value = o.trip;
        $d("serviceType").value = o.serviceType;
        $d("createBy").value = o.createBy;
        $d("createDate").value = o.createDate;
        $d("updateBy").value = o.updateBy;
        $d("updateDate").value = o.updateDate;
        $d("bSave").setAttribute("onclick","save()");
        
        getLookupList("SERVICE_TYPE","serviceType",o.serviceType);
    } catch(err) {
        alert("edit err:"+err);
    }
}

function switchProcess(type,msg,method){
    if(type == 0){//fail
        addSysMsg(0,msg);
    }else{
        addProcessing(msg);                
        setTimeout(method,2000);        
    }    
}
function save() { 
    
    try {
        var o = new CarRent();
        
        o.tableId = $d("tableId").value;
        o.carRentRateId = $d("carRentRateId").value;
        o.perHour = parseInt($d("perHour").value);
        o.minimumCharge = parseInt($d("minimumCharge").value);
        o.sedan = parseInt($d("sedan").value);
        o.van = parseInt($d("van").value);
        o.trip = parseInt($d("trip").value);
        o.serviceType = $d("serviceType").value;
        o.createBy = $d("createBy").value;
        o.createDate = $d("createDate").value;
        o.updateBy = $d("updateBy").value;
        o.updateDate = $d("updateDate").value;

        if( verify() == true ) {
            addProcessing('Please wait...');
            var ret = service.update(o);
            if( ret == "true" ) {         
                search(0);//refresh data
                switchProcess(1,"Save completed.","formSearch()");                
            } else {
                switchProcess(0,"Save fail.","");                
            }
        }
    } catch(err) {
        alert("save err:"+err);
    }
}
function remove(id) {
    try {
        service.remove(id);
        search(0);
    } catch(err) {
        alert("remove err:"+err);
    }
}
function search() {
    try {
        var ls = service.search(0, $d("tTableId").value);
        var jTable = new JTable();

        /*
        $d("driverId").value = o.driverId;
        $d("driverName").value = o.driverName;
        $d("driverType").value = o.driverType;
        $d("driverRate").value = o.driverRate;
        $d("perType").value = o.perType;
        $d("accountNo").value = o.accountNo;
        $d("bank").value = o.bank;
        $d("status").value = o.status;
        $d("createBy").value = o.createBy;
        $d("createDate").value = o.createDate;
        $d("updateBy").value = o.updateBy;
        $d("updateDate").value = o.updateDate;
        */

        jTable.setTableId("driverTable");
        jTable.setColumns(["driverName", "driverType", "driverRate", "perType"]);
        jTable.setFields(["driverName","driverType","driverRate", "perType"]);
        jTable.setEditFunction("edit");
        jTable.setDeleteFunction("remove");
        jTable.setDataId("driverId");
        jTable.setCount(ls.size());
        jTable.setOffset(0);
        jTable.setDataPageSize(20);
        jTable.setPageLink("pagelink");
        jTable.setTableHeader();
        jTable.loadData(ls);
    } catch(err) {
        alert("search err:"+err);
    }
}
function search(offset) {
    
    try {               
        addProcessing('Please wait...');
        var ls = service.search(offset,$d("tTableId").value);        
        var jTable = new JTable();
        jTable.setTableId("tableData");        
        jTable.setColumns(["CarRent Rate Id", "Per Hour","Minimum Charge","Sedan", "Van","Trip","Service Type", "Update By", "Update Date"]);
        jTable.setFields(["carRentRateId", "perHour","minimumCharge","sedan", "van","trip","serviceTypeName", "updateByName", "updateDate"]);
        jTable.setEditFunction("edit");
        jTable.setDeleteFunction("remove");
        jTable.setDataId("tableId");
        jTable.setCount(ls.size());
        jTable.setOffset(offset);
        jTable.setDataPageSize(20);
        jTable.setPageLink("search");        
        jTable.setTableHeader();
        jTable.loadData(ls);        
                
        
    } catch(err) {
        alert("search err:"+err);        
    }
    clearAllMsg();
}
function insert() { 
    
    try {
        var o = new CarRent();
        
        o.tableId = $d("tableId").value;
        o.carRentRateId = $d("carRentRateId").value;
        o.perHour = parseInt($d("perHour").value);
        o.minimumCharge = parseInt($d("minimumCharge").value);
        o.sedan = parseInt($d("sedan").value);
        o.van = parseInt($d("van").value);
        o.trip = parseInt($d("trip").value);
        o.serviceType = $d("serviceType").value;
        o.createBy = $d("createBy").value;
        o.createDate = $d("createDate").value;
        o.updateBy = $d("updateBy").value;
        o.updateDate = $d("updateDate").value;
        
        //        var re = service.save(lookup);
        if( verify() == true ) {
            addProcessing('Please wait...');
            var ret = service.save(o);
            if( ret == "true" ) {            
                switchProcess(1,"Add completed.","formSearch()");
            } else {            
                switchProcess(0,"Add fail.","");
            }
        }
    } catch(err) {
        alert("insert err:"+err);
    }
}
        
function setInsert() {    
    try {
        formEntry();
        var searchd = $d("tableId").value;
        $d("actionForm").reset();
        $d("tableId").value = searchd;
        $d("bSave").setAttribute("onclick","insert()");
        getLookupList("SERVICE_TYPE","serviceType","");
    } catch(err) {
        alert("setInsert err:"+err);
    }
}
         
function formEntry() {
    clearAllMsg();
    document.getElementById("fieldSetLabel").textContent="Car Rent Add";
    try {
        $d("formSearch").style.visibility = "hidden";
        var formEntry = $d("formEntry");
        formEntry.style.visibility = "visible";
        var panel = $d("panel");
        formEntry.style.top = panel.style.top;
        formEntry.style.left = panel.style.left;
    } catch(err) {
        alert("formEntry err:"+err);
    }
}
function formSearch() {
    clearAllMsg();
    document.getElementById("fieldSetLabel").textContent="Car Rent Search";    
    try {
        $d("formEntry").style.visibility = "hidden";
        var search = $d("formSearch");
        search.style.visibility = "visible";
        var panel = $d("panel");
        search.style.top = panel.style.top;
        search.style.left = panel.style.left;
    } catch(err) {
        alert("formSearch err:"+err);
    }
}
function verify() { 
    
    try {
        var v = new Validation();
        v.addField("name='tableId',label='tableId',min=1,max=21,type='string'");
        v.addField("name='carRentRateId',label='Car Rent RateId',min=1,max=21,type='string'");
        v.addField("name='perHour',label='Per Hour',min=0,max=3000,type='float',require=true");        
        v.addField("name='minimumCharge',label='Minimum Charge',min=0,max=3000,type='float',require=true");
        v.addField("name='sedan',label='Sedan',min=0,max=3000,type='float',require=true");
        v.addField("name='van',label='Van',min=0,max=3000,type='float',require=true");
        v.addField("name='trip',label='Trip',min=0,max=3000,type='float',require=true");
        v.addField("name='serviceType',label='Service Type',min=0,max=11,type='int',require=true");
                
        var str = v.execute();
        if ( str.length == 0 ) {
            return true;
        } else {
            addSysMsg(0,str);
            return false;
        }
    } catch(err) {
        alert(err);
    }
}
function getLookupList(lookupGroupId,propertieId,selectedValue) {    
    var list = serviceLookup.getLookupType(lookupGroupId);
    var i=0;
    for( i =0; i < list.size(); i++ ) {
        var txt = list.get(i).lookupName;
        var val = list.get(i).lookupValue;
        
        if(val==selectedValue){
            $d(propertieId).options[i] = new Option(txt, val, false, true);
        }else{
            $d(propertieId).options[i] = new Option(txt, val, false, false);
        }
    }   
}